#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
#include <QProgressBar>
#include "Protocol.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
/*
    pressThreshold = 1150;

    ui->pushButtonAdd->setStyleSheet(
                "QPushButton{"
                    "background-image:url(:res/add_normal.png);"
                    "padding:0;"
                );
*/
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::monitor()
{
    //串口初始化
    serialPort = new QSerialPort;
    //设置使用哪个串口
    serialPort->setPortName("COM2");
    //读写方式打开串口
    if(!serialPort->open(QIODevice::ReadWrite))
    {
        QMessageBox::critical(nullptr,"串口打开失败","串口COM2不存在或已被占用",QMessageBox::Ok);
    }
    //设置串口
    serialPort->setBaudRate(19200);               //波特率
    serialPort->setDataBits(QSerialPort::Data8);  //设置8位数据位
    serialPort->setParity(QSerialPort::NoParity); //不设置奇偶检验
    serialPort->setStopBits(QSerialPort::OneStop);//设置一位停止位
    serialPort->setFlowControl(QSerialPort::NoFlowControl); //不设置流动控制

    //准备好串口之后，打开收包线程
    receivePackThread = new ReceivePackThread(serialPort);
    receivePackThread->start();

    //（QtUI界面的更新在主线程里进行就好，在其他线程中进行非法，不安全）
    // 关联receive下位机信息后更新到UI界面的信号槽
    connect(receivePackThread,&ReceivePackThread::receivePackData,this,&MainWindow::updataUIData);

    //准备好串口之后，打开发包线程
    sendPackThread = new SendPackThread(serialPort);
    sendPackThread->start(); // start()执行线程重载的run()
}

void MainWindow::updataUIData(char cmd, char param, int data)
{
    switch(cmd)
    {
        case ZHU_JI_WEN_DU_RSP:
            ui->labelWendu->setText(QString::number(data));
            break;
        case ZHOU_SU_DU_RSP:
            {
                if(param < 1 || param > 2) break;
                QLabel *labels[2] = {ui->labelZhuzhou,ui->labelFuzhou};
                labels[param-1]->setText(QString::number(data));
                break;
            }
        case GANG_YOU_LIANG_RSP:
            {
                if(param < 1 || param > 3) break;
                QLabel *labels[3] = {ui->labelGang_1,ui->labelGang_2,ui->labelGang_3};
                labels[param-1]->setText(QString::number(data) + "L");
                break;
            }
        case YA_LI_CANG_RSP:
            {
                if(param < 1 || param > 5) break;
                QWidget *widgets[5][2] = {
                    {ui->progressBar_Cang_1,ui->labelCang_1},
                    {ui->progressBar_Cang_2,ui->labelCang_2},
                    {ui->progressBar_Cang_3,ui->labelCang_3},
                    {ui->progressBar_Cang_4,ui->labelCang_4},
                    {ui->progressBar_Cang_5,ui->labelCang_5}
                };

                QProgressBar *bar = (QProgressBar*)widgets[param-1][0];
                bar->setValue(data);
                QLabel *label = (QLabel*)widgets[param-1][1];
                label->setText(QString::number(data) + "Pa");

                break;
            }
    case JI_XIE_BI_RSP:
            ui->labelJixiebi->setText(QString::number(data) + "Pa");
            ui->progressBarJixiebi->setValue(data);
            break;

    }
}

